package com.example.flashsales.service.impl;

import com.example.flashsales.entity.dto.ZhekouDto;
import com.example.flashsales.entity.vo.ZhekouVo;
import com.example.flashsales.mappers.ZhekouMapper;
import com.example.flashsales.service.ZhekouService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class ZhekouServiceImpl implements ZhekouService {

    @Autowired
    ZhekouMapper zhekouMapper;

    /**
     * 创建卷
     * @param zhekouDto
     * @return
     */
    @Override
    @Transactional
    public boolean createVolume(ZhekouDto zhekouDto) {
        boolean flag = false;
        for (int i = 0; i < zhekouDto.getCount(); i++) {
            if (zhekouMapper.createVolume(zhekouDto) <= 0) {
                flag = true;
                break;
            }
        }
        if (flag){
            return false;
        }
        return true;
    }

    /**
     * 查看卷
     * @param state
     * @param name
     * @param create_time
     * @param end_time
     * @param page
     * @param num
     * @return
     */
    @Override
    public List<ZhekouVo> viewVolume(Integer state, String name, Date create_time, Date end_time, Integer page, Integer num,Long id,Long orderId,Long userId) {
        return zhekouMapper.viewVolume(state,name,create_time,end_time,id,orderId,userId);
    }

    /**
     * 禁用卷
     * @param list
     * @return
     */
    @Override
    @Transactional
    public boolean disableVolume(List<Long> list) {
        for (int i = 0; i < list.size(); i++) {
            if (zhekouMapper.updateVolume(list.get(i),4) <= 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 发布卷
     * @param list
     * @return
     */
    @Override
    @Transactional
    public boolean releaseVolume(List<Long> list) {
        for (int i = 0; i < list.size(); i++) {
            if (zhekouMapper.updateVolume(list.get(i),5) <= 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * 用户领取卷
     * @param id
     * @param zid
     * @return
     */
    @Override
    public boolean drawVoluem(int id, Long zid) {
//        System.out.println(id+"-"+zid);
        return zhekouMapper.drawVolume(id,zid) > 0;
    }


    /**
     * 过期
     * @return
     */
    @Transactional
    public List<Long> changeVolume() {
        final List<Long> longs = zhekouMapper.viewGvolume();
        for (int i = 0; i < longs.size(); i++) {
            if (zhekouMapper.updateVolume(longs.get(i),2) <= 0) {
                return null;
            }
        }
        return longs;
    }
}
